package com.five.reader.service.impl;

import com.five.exception.BusinessException;
import com.five.pojo.Reader;
import com.five.reader.mapper.ReaderMapper;
import com.five.reader.service.ReaderService;
import com.five.reader.vo.ReaderVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


@Service
@RequiredArgsConstructor
public class ReaderServiceImpl implements ReaderService {

    private final ReaderMapper readerMapper;

    /**
     * 根据ID获取读者信息
     * @param readerId 读者ID
     * @return 读者信息
     */
    @Override
    public ReaderVO getReaderInfo(Long readerId) {
        Reader reader = readerMapper.selectById(readerId);
        if (reader == null) {
            throw new BusinessException("读者不存在");
        }
        return convertToVO(reader);
    }

    /**
     * 更新读者信息 - 只更新业务信息，不更新登录相关字段
     * @param reader 读者信息
     * @return 更新结果
     */
    @Override
    @Transactional
    public boolean updateReaderInfo(Reader reader) {
        Reader existingReader = readerMapper.selectById(reader.getId());
        if (existingReader == null) {
            throw new BusinessException("读者不存在");
        }
        
        // 只更新允许的业务字段
        if (reader.getNickname() != null) {
            existingReader.setNickname(reader.getNickname());
        }
        if (reader.getAvatar() != null) {
            existingReader.setAvatar(reader.getAvatar());
        }
        if (reader.getGender() != null) {
            existingReader.setGender(reader.getGender());
        }
        if (reader.getPhone() != null) {
            existingReader.setPhone(reader.getPhone());
        }
        if (reader.getEmail() != null) {
            existingReader.setEmail(reader.getEmail());
        }
        
        return readerMapper.update(existingReader) > 0;
    }

    /**
     * 更新读者最后登录时间 - 由user-service调用
     * @param readerId 读者ID
     * @return 更新结果
     */
    @Override
    @Transactional
    public boolean updateLastLoginTime(Long readerId) {
        return readerMapper.updateLastLoginTime(readerId,LocalDateTime.now()) > 0;
    }

    /**
     * 根据用户名获取读者信息
     * @param username 用户名
     * @return 读者信息
     */
    @Override
    public ReaderVO getReaderByUsername(String username) {
        Reader reader = readerMapper.selectByUsername(username);
        if (reader == null) {
            throw new BusinessException("读者不存在");
        }
        return convertToVO(reader);
    }

    /**
     * 根据手机号获取读者信息
     * @param phone 手机号
     * @return 读者信息
     */
    @Override
    public ReaderVO getReaderByPhone(String phone) {
        Reader reader = readerMapper.selectByPhone(phone);
        if (reader == null) {
            throw new BusinessException("读者不存在");
        }
        return convertToVO(reader);
    }

    /**
     * 根据邮箱获取读者信息
     * @param email 邮箱
     * @return 读者信息
     */
    @Override
    public ReaderVO getReaderByEmail(String email) {
        Reader reader = readerMapper.selectByEmail(email);
        if (reader == null) {
            throw new BusinessException("读者不存在");
        }
        return convertToVO(reader);
    }

    /**
     * 更新读者密码
     * @param readerId 读者ID
     * @param newPassword 新密码
     * @param salt 盐值
     * @return 更新结果
     */
    @Override
    @Transactional
    public boolean updatePassword(Long readerId, String newPassword, String salt) {
        return readerMapper.updatePassword(readerId, newPassword, salt) > 0;
    }

    /**
     * 更新读者状态
     * @param readerId 读者ID
     * @param status 状态 (0:禁用, 1:正常)
     * @return 更新结果
     */
    @Override
    @Transactional
    public boolean updateStatus(Long readerId, Integer status) {
        return readerMapper.updateStatus(readerId, status) > 0;
    }

    /**
     * 删除读者（逻辑删除）
     * @param readerId 读者ID
     * @return 删除结果
     */
    @Override
    @Transactional
    public boolean deleteReader(Long readerId) {
        return readerMapper.deleteById(readerId) > 0;
    }



    /**
     * 将 Reader 实体转换为 ReaderVO
     * @param reader 读者实体
     * @return 读者VO对象
     */
    private ReaderVO convertToVO(Reader reader) {
        ReaderVO vo = new ReaderVO();
        BeanUtils.copyProperties(reader, vo);
        return vo;
    }
}